package com.example.teamtribe.main.club;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.teamtribe.R;
import com.example.teamtribe.base.BaseFragment;
import com.example.teamtribe.database.Team;
import com.example.teamtribe.event.Refresh;
import com.example.teamtribe.main.club.manage.TopManageActivity;
import com.example.teamtribe.main.club.setup.SetUpActivity;
import com.example.teamtribe.main.mine.setting.certify.CertifyActivity;
import com.example.teamtribe.utils.Utility;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Collection;

public class ClubFragment extends BaseFragment implements View.OnClickListener, ClubContract.View {

    private final int REQUEST = 1;
    private final int SET_UP = 2;

    private ImageView ivAdd;
    private ImageView ivManage;
    private SmartRefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private Button btCertify;
    private ClubGridAdapter adapter;
    private int schoolId;
    private ClubContract.Presenter presenter;

    @Override
    protected int setContentView() {
        return R.layout.fragment_club;
    }

    @Override
    protected void onCreateView(View view) {
        ivManage = view.findViewById(R.id.iv_back);
        ((TextView) view.findViewById(R.id.tv_title)).setText("社团");
        schoolId = Utility.getUser().getSchoolId();
        presenter = new ClubPresenter(this);
        ivAdd = view.findViewById(R.id.iv_more);
        ivAdd.setImageDrawable(getResources().getDrawable(R.drawable.ic_add_circle));
        ivManage.setImageResource(R.drawable.ic_manage);
        ivAdd.setOnClickListener(this);
        ivManage.setOnClickListener(this);
        isManager();
        btCertify = view.findViewById(R.id.bt_certify);
        btCertify.setOnClickListener(this);

        recyclerView = view.findViewById(R.id.rv_club_list);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
        recyclerView.setNestedScrollingEnabled(false);
        adapter = new ClubGridAdapter();
        recyclerView.setAdapter(adapter);
        refreshLayout = view.findViewById(R.id.refreshLayout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                presenter.getTeams(schoolId);
            }
        });
        presenter.start(schoolId);
        if(!EventBus.getDefault().isRegistered(this)){//加上判断
            EventBus.getDefault().register(this);
        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST && resultCode == Activity.RESULT_OK) {
            schoolId = Utility.getUser().getSchoolId();
            isManager();
            presenter.start(schoolId);
            return;
        }
        if (requestCode == SET_UP && resultCode == Activity.RESULT_OK) {
            refreshLayout.autoRefresh();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                startActivity(new Intent(getContext(), TopManageActivity.class));
                break;

            case R.id.iv_more:
                startActivityForResult(new Intent(getContext(), SetUpActivity.class), SET_UP);
                break;

            case R.id.bt_certify:
                startActivityForResult(new Intent(getContext(), CertifyActivity.class), REQUEST);
                break;

            default:
                break;
        }
    }

    @Override
    public void toCertify(final boolean isShow) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (isShow) {
                    btCertify.setVisibility(View.VISIBLE);
                } else {
                    btCertify.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    public void addTeam(final Team team) {
        adapter.add(team);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void addTeams(final Collection<Team> teams) {
        adapter.addAll(teams);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void setPortrait() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void clearTeams() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.clear();
            }
        });
    }

    @Override
    public void finishRefresh() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                refreshLayout.finishRefresh();
            }
        });
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refesh(Refresh refresh) {
        if(refresh.getType()==Refresh.CLUBFRAGMENT){
            presenter.getTeams(schoolId);
            Log.d("refresh", "clubfragment-refesh: ");
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("refresh", "onDestroy: ");
        EventBus.getDefault().unregister(this);
    }

    private void isManager() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (schoolId != 0 && Utility.getUser().getRealId() == 1) {//学校社团管理员，长沙理工是1，如果有别的学校要获取
                    ivAdd.setVisibility(View.VISIBLE);
                    ivManage.setVisibility(View.VISIBLE);
                } else {
                    ivAdd.setVisibility(View.GONE);
                    ivManage.setVisibility(View.GONE);
                }
            }
        });
    }
}